package com.ruoyi.project.hissystem.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hissystem.domain.HisDepartment;
import com.ruoyi.project.hissystem.service.HisDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 杨磊
 * @date : 22:01 2021/8/17
 */

@RestController
@RequestMapping("/hissystem/department")
public class HisDepartmentController extends BaseController {
    
    //声明业务层属性 
    @Autowired
    private HisDepartmentService hisDepartmentService;

     /**
     * 获取科室列表
     */
    @PreAuthorize("@ss.hasPermi('hissystem:department:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisDepartment hisDepartment)  //TableDataInfo  相当于以前的PageInfo
    {
        startPage();  //开启分页 （里面会拿页码数和每页显示的数量 ）  /*它里面创建一个对象  用set方法给属性赋值   底层还是用request对象 拿请求的键名取值*/
        List<HisDepartment> hisDepartments = hisDepartmentService.selectDepartmentList(hisDepartment);
        return getDataTable(hisDepartments);
    }

    /**
     * 新增科室信息
     */
    @PreAuthorize("@ss.hasPermi('hissystem:department:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)  // @Log用来打日志的
    @PostMapping
    // @Validated  校验的注解  一般配合实体类上面的@NotBlank 注解 因为我的实体类还没有配 所以这里就先删掉了 这个注解
    /*当请求数据来的时候，要把Json（不是键值对） 注入到实体类中 注入的时候就会触发注解 这个注解再触发实体类上面的注解*/
    public AjaxResult add(@RequestBody HisDepartment hisDepartment)

    {

        hisDepartment.setCreateBy(SecurityUtils.getUsername());  // 拿到当前人登录的信息  因为创建者是登录的人（setCreateBy）
        return toAjax(hisDepartmentService.insertDepartment(hisDepartment));
    }

     /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('hissystem:department:query')")
    @GetMapping(value = "/{departId}")
    public AjaxResult getInfo(@PathVariable Long departId)
    {
        return AjaxResult.success(hisDepartmentService.selectDepartmentById(departId));
    }

    /**
     * 修改科室信息
     */
    @PreAuthorize("@ss.hasPermi('hissystem:department:edit')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody  HisDepartment hisDepartment)
    {

        hisDepartment.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(hisDepartmentService.updateDepartment(hisDepartment));
    }

    /**
     * 删除科室
     */
    @PreAuthorize("@ss.hasPermi('hissystem:department:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{departIds}")
    public AjaxResult remove(@PathVariable Long[] departIds)
    {
        return toAjax(hisDepartmentService.deleteDepartmentByIds(departIds));
    }

    /**
     * 科室导出
     * @param hisDepartment 科室信息
     * @return
     */
    
    @Log(title = "科室管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('hissystem:department:export')")
    @GetMapping("/export")
    public AjaxResult export(HisDepartment hisDepartment)
    {
        List<HisDepartment> hisDepartments = hisDepartmentService.selectDepartmentList(hisDepartment);
        ExcelUtil<HisDepartment> util = new ExcelUtil<HisDepartment>(HisDepartment.class);
        return util.exportExcel(hisDepartments, "科室数据");
    }
    

}
